package com.krmes.mes;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.krmes.mes.app.AppContext;
import com.krmes.mes.app.AppManager;
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

import cz.msebera.android.httpclient.Header;


public class ChangePwdActivity extends FragmentActivity
        implements OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_changepwd);
        AppManager.getAppManager().addActivity(this);
        initView();
    }
    private EditText et_password_was;
    private EditText et_password_new;
    private EditText et_password_cof;
    private Button button_ok;
    private Button button_cel;

    private void initView()
    {
        this.button_ok = ((Button)findViewById(R.id.button_ok));
        this.button_ok.setOnClickListener(this);
        this.button_cel = ((Button)findViewById(R.id.button_cel));
        this.button_cel.setOnClickListener(this);
        this.et_password_was = ((EditText)findViewById(R.id.editText_waspwd));
        this.et_password_new = ((EditText)findViewById(R.id.editText_newpwd));
        this.et_password_cof = ((EditText)findViewById(R.id.editText_cofpwd));
    }

    private boolean validate()
    {
        if ((this.et_password_was.getText() == null) || (this.et_password_was.getText().toString().equals("")))
        {
            AppContext.showToast("请输入原密码");
            this.et_password_was.requestFocus();
            return false;
        }
        if ((this.et_password_new.getText() == null) || (this.et_password_new.getText().toString().equals("")))
        {
            AppContext.showToast("请输入新密码");
            this.et_password_new.requestFocus();
            return false;
        }
        if ((this.et_password_cof.getText() == null) || (this.et_password_cof.getText().toString().equals("")))
        {
            AppContext.showToast("请输入确认密码");
            this.et_password_cof.requestFocus();
            return false;
        }
        if ((this.et_password_cof.getText().toString().equals(this.et_password_new.getText().toString())) ==false)
        {
            AppContext.showToast("新密码与确认密码不一致,请重新输入!");
            this.et_password_cof.setText("");
            this.et_password_cof.requestFocus();
            return false;
        }
        return true;
    }

    @Override
    public void onClick(View paramView) {
        if (paramView.getId() == R.id.button_ok) {
            this.button_ok.setClickable(false);
            if (validate()) {
                ServerApi.changePwd(AppContext.loginCode,this.et_password_was.getText().toString(), this.et_password_new.getText().toString(), this.et_password_cof.getText().toString(), new AsyncHttpResponseHandler()
                {
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, byte[] pArrayOfByte) {
                        try
                        {
                            JSONObject jsonResult = new JSONObject(new String(pArrayOfByte, "UTF-8"));
                            int i = jsonResult.optInt("ret_code");
                            String str = jsonResult.optString("err_msg");
                            AppContext.showToast(str);
                            ChangePwdActivity.this.button_ok.setClickable(true);
                            if (i == 0)
                            {
//                                String[] arrayOfString = jsonResult.optString("data").split(",");
//                                AppContext.loginCode = arrayOfString[0];
//                                AppContext.userName = arrayOfString[1];
//                                AppContext.roleCode = arrayOfString[2];
//                                AppContext.roleName = arrayOfString[3];
//                                Intent intent = new Intent(ChangePwdActivity.this, MainActivity.class);
//                                ChangePwdActivity.this.startActivity(intent);
                                ChangePwdActivity.this.finish();
                                return;
                            }

                            return;
                        }
                        catch (UnsupportedEncodingException localUnsupportedEncodingException)
                        {
                            localUnsupportedEncodingException.printStackTrace();
                            return;
                        }
                        catch (JSONException localJSONException)
                        {
                            localJSONException.printStackTrace();
                        }

                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable pThrowable) {
                        ChangePwdActivity.this.button_ok.setClickable(true);
                        AppContext.showToast(pThrowable.getMessage());
                        pThrowable.printStackTrace();
                        pThrowable.toString();
                        Log.d("statusCode",String.valueOf(statusCode));

                    }


                });
            }else{
                this.button_ok.setClickable(true);
            }
        }
        if (paramView.getId() == R.id.button_cel) {
            finish();
            return;
        }

    }
}
